﻿#include <QCoreApplication>
#include <QThread>
#include <QtConcurrent/QtConcurrent>
#include <iostream>
#include <string>
#include <thread>

#include "co/all.h"
#include "zmq.hpp"

int main(int argc, char **argv) {
  QCoreApplication a{argc, argv};

  std::string request{"hello"};
  //  request.resize(1024 * 1024);

  QtConcurrent::run([=] {
    zmq::context_t context(1);
    zmq::socket_t socket(context, zmq::socket_type::req);

    socket.connect("tcp://127.0.0.1:5555");

    while (true) {
      QThread::msleep(1000);

      auto beforeTime = std::chrono::steady_clock::now();

      // wait until the socket is ready for sending, or timeout occurs
      auto tempSize = socket.send(zmq::buffer(request), zmq::send_flags::none);
      std::cout << "send:" << tempSize.has_value() << ' ' << tempSize.value()
                << std::endl;

      auto afterTime = std::chrono::steady_clock::now();
      //毫秒级
      double duration_millsecond =
          std::chrono::duration<double, std::milli>(afterTime - beforeTime)
              .count();
      std::cout << duration_millsecond << "毫秒" << std::endl;

      zmq::message_t reply;
      socket.recv(reply, zmq::recv_flags::none);
      std::string reply_string(static_cast<char *>(reply.data()), reply.size());
      std::cout << "Received reply: " << reply_string << std::endl;
    }
  });

  return a.exec();
}
